package com.hod.client.charge;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataObject;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.chargedescription.ChargeDescriptionCRUD;
import com.hod.client.frontend.HODForm;
import com.hod.client.lodging.LodgingCRUD;

public class ChargeFormPanel extends HODForm {
	
    private Integer idLodging = 0;
    private Integer idChargeDescription = 0;
    
	protected ListBox txtIdLodging = new ListBox();
    protected ListBox txtIdChargeDescription = new ListBox();
    protected final TextField txtIdCharge = new TextField("idCharge", "idCharge", 150);
    protected final DateField txtDate = new DateField("date","date",60);
    protected final NumberField txtTotal = new NumberField("total","total",60);

	public ChargeFormPanel(GearsDataStore ds) {
		super(ds);
		// TODO Auto-generated constructor stub
		add(createFieldSet());        
        createButtons();
	}

	@Override
	protected void createButtons() {
		// TODO Auto-generated method stub
		ButtonListenerAdapter savelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		Charge o = null;
            		if (txtIdCharge.getText()==""){
            			o = new Charge();
            			o.setIdCharge(null);
            		}else{
            			o = (Charge) ds.findById(Integer.valueOf(txtIdCharge.getText()));
            		}
            		o.setIdChargeDescription(Integer.valueOf(txtIdChargeDescription.getValue(txtIdChargeDescription.getSelectedIndex())));
            		o.setIdLodging(Integer.valueOf(txtIdLodging.getValue(txtIdLodging.getSelectedIndex())));
            		o.setDate(txtDate.getValue());
            		o.setTotal(Double.valueOf(txtTotal.getValueAsString()));
                    ds.save(o);
                    HOD.reload();
				} 
            	catch (Exception e1) {
					// TODO Auto-generated catch block
					Window.alert(e1.getMessage());
				}
            }
        };
        ButtonListenerAdapter deletelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		ds.delete((Charge) ds.findById(Integer.valueOf(txtIdCharge.getText())));
            		HOD.reload();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					Window.alert(e1.getMessage());
				}
            }
        };
        ButtonListenerAdapter createlistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		getForm().reset();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					Window.alert(e1.getMessage());
				}
            }
        };
        	           
        
        Button save = new Button("Save", savelistener);
        Button delete = new Button("Delete", deletelistener);
		Button newRecord = new Button("New", createlistener);
        Button cancel = new Button("Cancel");

        addButton(save);
        addButton(delete);
        addButton(cancel);
        addButton(newRecord);
		

	}

	@Override
	protected FieldSet createFieldSet() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		txtIdCharge.setReadOnly(true);
		txtIdCharge.setVisible(false);
		txtIdCharge.setHideLabel(true);
		
		txtDate.setAllowBlank(false);
		txtTotal.setAllowBlank(true);
		txtTotal.setAllowDecimals(true);
		txtTotal.setAllowNegative(true);
		
		FieldSet fieldSet = new FieldSet();
        fieldSet.setLabelWidth(80);
        fieldSet.setTitle("Charge details");
        fieldSet.setAutoHeight(true);
        fieldSet.setBorder(false);

        LodgingCRUD.createListBox(idLodging,txtIdLodging);
        ChargeDescriptionCRUD.createListBox(idChargeDescription,txtIdChargeDescription);
        
        //the field names must match the data field values from the Store
        fieldSet.add(txtIdCharge);
        fieldSet.add(new HTML("<BR/>"));
        fieldSet.add(new Label("Codigo Hospedaje"));        
        fieldSet.add(txtIdLodging);
        fieldSet.add(new HTML("<BR/>"));
        fieldSet.add(new Label("Cargo"));
        fieldSet.add(txtIdChargeDescription);
        fieldSet.add(txtDate);
        fieldSet.add(txtTotal);
        return fieldSet;
	}

	@Override
	public void updateDataControls(Record record) {
		// TODO Auto-generated method stub
		 getForm().loadRecord(record);      
 		 idLodging = record.getAsInteger("idLodging");
		 idChargeDescription = record.getAsInteger("idChargeDescription");
         LodgingCRUD.createListBox(idLodging,txtIdLodging);
         ChargeDescriptionCRUD.createListBox(idChargeDescription,txtIdChargeDescription);

	}
	
	public void setIdLodging(Integer idLodging) {
		this.idLodging = idLodging;
	}

	public Integer getIdLodging() {
		return idLodging;
	}

	public void setIdChargeDescription(Integer idChargeDescription) {
		this.idChargeDescription = idChargeDescription;
	}

	public Integer getIdChargeDescription() {
		return idChargeDescription;
	}

	@Override
	public void setObject(GearsDataObject object) {
		// TODO Auto-generated method stub
		
	}

}
